<?php
namespace app\admin\logic;

use app\common\logic\Base;
use think\facade\App;

class AuthMember extends Base {

    /**
     * @desc 后台用户列表页
     * @param array $params
     * @return array
     */
    public function index($params) {
        $where = [];
        if($params['username']) {
            $where[] = ['am_username', '=', $params['username']];
        }

        $field = [
            'am_member_id as memberId',
            'am_username as username',
            'am_status as status',
            'am_create_time as createTime',
            'am_remark as remark',
            'ag_name as groupName'
        ];
        $list = App::model('AuthMember')
            ->alias('am')
            ->leftJoin('AuthGroup ag', 'ag.ag_id = am.am_group_id')
            ->field($field)
            ->order('am_member_id', 'desc')
            ->where($where)
            ->select();

        return [
            'list' => $list
        ];
    }

    /**
     * @desc 添加用户
     * @param $params
     * @return array
     */
    public function add($params) {
        $groupList = App::model('AuthGroup')
            ->field('ag_id as id,ag_name as name')
            ->select();

        return [
            'params'    => $params,
            'groupList' => $groupList
        ];
    }

    /**
     * @desc 编辑用户
     * @param $params
     * @return array
     */
    public function edit($params) {
        $field = [
            'am_member_id as memberId',
            'am_username as username',
            'am_salt as salt',
            'am_status as status',
            'am_group_id as groupId',
        ];
        $list = App::model('AuthMember')
            ->field($field)
            ->where(['am_member_id' => $params['memberId']])
            ->find();

        $groupList = App::model('AuthGroup')
            ->field('ag_id as id,ag_name as name')
            ->select();

        return [
            'info'      => $list,
            'params'    => $params,
            'groupList' => $groupList
        ];
    }

    /**
     * @desc 用户操作 (添加、编辑)
     * @param array $params
     * @return array|bool
     */
    public function operateAjax($params) {
        $data = [
            'am_username' => $params['username'],
            'am_status'   => $params['status'],
            'am_group_id' => $params['groupId'],
            'am_remark'   => $params['remark'],
        ];
        if($params['action'] == 'add') {
            if(empty($params['password'])) {
                $this->errorCode = EC_PARAMS_ILLEGAL;
                return false;
            }
            $username = App::model('AuthMember')
                ->where('am_username', '=', $params['username'])
                ->value('am_username');
            if($username) {
                $this->errorCode = EC_AD_AUTH_MEMBER_USERNAME_ALREADY_EXIST;
                return false;
            }

            $data['am_salt'] = random_string(6);
            $data['am_password'] = $this->generatePassword($params['password'], $data['am_salt']);
            $result = App::model('AuthMember')->insertGetId($data);
        }else {
            $info = App::model('AuthMember')
                ->where(['am_member_id' => $params['memberId']])
                ->find();

            if(!$info) {
                $this->errorCode = EC_AD_AUTH_MEMBER_USERNAME_NOT_EXIST;
                return false;
            }

            if($params['password']) {
                $data['am_password'] = $this->generatePassword($params['password'], $info['am_salt']);
            }

            $result = App::model('AuthMember')
                ->where(['am_member_id' => $params['memberId']])
                ->update($data);
        }

        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }

    /**
     * @desc 生成密码
     * @param string $password 明文密码
     * @param string $salt 干扰码
     * @return bool
     */
    public function generatePassword($password, $salt) {
        $password = md5($password. $salt);
        return $password;
    }
}